home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / game / think / AmiChess.lha / AmiChess / src / epd.c < prev    next >
C/C++ Source or Header  |  2002-10-31  |  5KB  |  228 lines

  1. #include <clib/alib_protos.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include "common.h"
  6.  
  7. #define EPDCLOSE 1
  8.  
  9. short ReadEPDFile(const char *file,short op)
  10. {
  11. static FILE *fp=0;
  12. char line[255];
  13. if(!fp)
  14.     {
  15.     if(!(fp=fopen(file,"r")))
  16.         {
  17.         sprintf(line,"Error opening file %s",file);
  18.         DoMethod(mui_app,MUIM_Chess_ShowThinking,line);
  19.         return false;
  20.         }
  21.     }
  22. if(op==EPDCLOSE)   
  23.     { 
  24.     fclose(fp);
  25.     fp=0;
  26.     return false;
  27.     }
  28. fgets(line,255,fp);
  29. if(!feof(fp)) 
  30.     {
  31.     ParseEPD(line);
  32. //    if(op!=2) printf("\n%s : Best move=%s\n",id,solution);
  33.     return true;
  34.     }
  35. fclose(fp);
  36. fp=0;
  37. return false;
  38. }
  39.  
  40. void ParseEPD(char *p)
  41. {
  42. short r,c,sq;
  43. char s[8];
  44. r=56;
  45. c=0;
  46. memset(&board,0,sizeof(board));
  47. while(*p!=' ')
  48.     {
  49.     sq=r+c;
  50.     switch(*p)
  51.         {
  52.         case 'P':
  53.             SETBIT(board.b[white][pawn],sq);
  54.             SETBIT(board.blockerr90,r90[sq]);
  55.             SETBIT(board.blockerr45,r45[sq]);
  56.             SETBIT(board.blockerr315,r315[sq]);
  57.             board.material[white]+=ValueP;
  58.             break;    
  59.         case 'N' :
  60.             SETBIT(board.b[white][knight],sq);
  61.             SETBIT(board.blockerr90,r90[sq]);
  62.             SETBIT(board.blockerr45,r45[sq]);
  63.             SETBIT(board.blockerr315,r315[sq]);
  64.             board.material[white]+=ValueN;
  65.             break;    
  66.         case 'B':
  67.             SETBIT(board.b[white][bishop],sq);
  68.             SETBIT(board.blockerr90,r90[sq]);
  69.             SETBIT(board.blockerr45,r45[sq]);
  70.             SETBIT(board.blockerr315,r315[sq]);
  71.             board.material[white]+=ValueB;
  72.             break;
  73.         case 'R':SETBIT(board.b[white][rook],sq);
  74.             SETBIT(board.blockerr90,r90[sq]);
  75.             SETBIT(board.blockerr45,r45[sq]);
  76.             SETBIT(board.blockerr315,r315[sq]);
  77.             board.material[white]+=ValueR;
  78.             break;    
  79.         case 'Q' :
  80.             SETBIT(board.b[white][queen],sq);
  81.             SETBIT(board.blockerr90,r90[sq]);
  82.             SETBIT(board.blockerr45,r45[sq]);
  83.             SETBIT(board.blockerr315,r315[sq]);
  84.             board.material[white]+=ValueQ;
  85.             break;    
  86.         case 'K':
  87.             SETBIT(board.b[white][king],sq);
  88.             SETBIT(board.blockerr90,r90[sq]);
  89.             SETBIT(board.blockerr45,r45[sq]);
  90.             SETBIT(board.blockerr315,r315[sq]);
  91.             break;    
  92.         case 'p':
  93.             SETBIT(board.b[black][pawn],sq);
  94.             SETBIT(board.blockerr90,r90[sq]);
  95.             SETBIT(board.blockerr45,r45[sq]);
  96.             SETBIT(board.blockerr315,r315[sq]);
  97.             board.material[black]+=ValueP;
  98.             break;    
  99.         case 'n':
  100.             SETBIT(board.b[black][knight],sq);
  101.             SETBIT(board.blockerr90,r90[sq]);
  102.             SETBIT(board.blockerr45,r45[sq]);
  103.             SETBIT(board.blockerr315,r315[sq]);
  104.             board.material[black]+=ValueN;
  105.             break;    
  106.         case 'b':
  107.             SETBIT(board.b[black][bishop],sq);
  108.             SETBIT(board.blockerr90,r90[sq]);
  109.             SETBIT(board.blockerr45,r45[sq]);
  110.             SETBIT(board.blockerr315,r315[sq]);
  111.             board.material[black]+=ValueB;
  112.             break;    
  113.         case 'r':
  114.             SETBIT(board.b[black][rook],sq);
  115.             SETBIT(board.blockerr90,r90[sq]);
  116.             SETBIT(board.blockerr45,r45[sq]);
  117.             SETBIT(board.blockerr315,r315[sq]);
  118.             board.material[black]+=ValueR;
  119.             break;    
  120.         case 'q':
  121.             SETBIT(board.b[black][queen],sq);
  122.             SETBIT(board.blockerr90,r90[sq]);
  123.             SETBIT(board.blockerr45,r45[sq]);
  124.             SETBIT(board.blockerr315,r315[sq]);
  125.             board.material[black]+=ValueQ;
  126.             break;    
  127.         case 'k':
  128.             SETBIT(board.b[black][king],sq);
  129.             SETBIT(board.blockerr90,r90[sq]);
  130.             SETBIT(board.blockerr45,r45[sq]);
  131.             SETBIT(board.blockerr315,r315[sq]);
  132.             break;    
  133.         case '/':
  134.             r-=8;
  135.             c=-1;
  136.         }
  137.     if(isdigit(*p)) c+=(*p-'0');
  138.     else c++;
  139.     p++;
  140.     }
  141. board.pmaterial[white]=board.material[white]-nbits(board.b[white][pawn])*ValueP;
  142. board.pmaterial[black]=board.material[black]-nbits(board.b[black][pawn])*ValueP;
  143. board.king[white]=leadz(board.b[white][king]);
  144. board.king[black]=leadz(board.b[black][king]);
  145. UpdateFriends(); 
  146. UpdateCBoard();
  147. UpdateMvboard();
  148. sscanf(p," %s %[^\n]",s,p);
  149. if(s[0]=='w') board.side=white;
  150. else if(s[0]=='b') board.side=black;
  151. sscanf(p," %s %[^\n]",s,p);
  152. if(strchr(s,'K')) board.flag|=WKINGCASTLE;
  153. if(strchr(s,'Q')) board.flag|=WQUEENCASTLE;
  154. if(strchr(s,'k')) board.flag|=BKINGCASTLE;
  155. if(strchr(s,'q')) board.flag|=BQUEENCASTLE;
  156. sscanf(p," %s %[^\n]",s,p);
  157. if(s[0]!='-') board.ep =(s[0]-'a')+(s[1]-'1')*8;
  158. else board.ep=-1;
  159. if(!strncmp(p,"bm",2)) sscanf(p,"%*s %[^;]; %[^\n]",solution,p); 
  160. if(!strncmp(p,"id",2)) sscanf(p,"%*s %[^;]; %[^\n]",id,p); 
  161. CalcHashKey();
  162. phase=PHASE;
  163. }
  164.  
  165. void LoadEPD(char *p)
  166. {
  167. char file[32];
  168. short N=1;
  169. sscanf(p,"%s %hd ",file,&N);
  170. if(strcmp(file,"next")==0) ReadEPDFile(file,0);
  171. else
  172.     { 
  173.     ReadEPDFile(file,1);
  174.     while(--N)
  175.         {
  176.         if(ReadEPDFile(file,2)==false)
  177.             {
  178.             DoMethod(mui_app,MUIM_Chess_ShowThinking,"File position exceeded.");
  179.             return; 
  180.             }
  181.         }
  182.     ReadEPDFile(file,0);
  183.     }
  184. DoMethod(mui_app,MUIM_Chess_ShowBoard);
  185. NewPosition();
  186. }
  187.  
  188. void SaveEPD(char *file)
  189. {
  190. FILE *fp;
  191. short r,c,sq,k;
  192. char c1;
  193. if(fp=fopen(file,"a"))
  194.     {
  195.     for(r=A8;r>=A1;r-=8)
  196.         {
  197.         k=0;
  198.         for(c=0;c<8;c++)
  199.             {
  200.             sq=r+c;
  201.             if(cboard[sq]==empty) k++;
  202.             else
  203.                 {
  204.                 if(k) fprintf(fp,"%1d",k);
  205.                 k=0;
  206.                 c1=notation[cboard[sq]];
  207.                 if(BitPosArray[sq] & board.friends[black])
  208.                 c1=tolower(c1);
  209.                 fprintf(fp,"%c",c1);
  210.                 }
  211.             }
  212.         if(k) fprintf(fp,"%1d",k);
  213.         if(r>A1) fprintf(fp,"/");
  214.         }
  215.     fprintf(fp,(board.side==white?" w ":" b "));
  216.     if(board.flag & WKINGCASTLE) fprintf(fp,"K");
  217.     if(board.flag & WQUEENCASTLE) fprintf(fp,"Q");
  218.     if(board.flag & BKINGCASTLE) fprintf(fp,"k");
  219.     if(board.flag & BQUEENCASTLE) fprintf(fp,"q");
  220.     if(!(board.flag&(WCASTLE|BCASTLE))) fprintf(fp,"-");
  221.     fprintf(fp," %s",(board.ep>-1?algbr[board.ep]:"-"));
  222.     fprintf(fp," bm 1; id 1;");
  223.     fprintf(fp,"\n");
  224.     fclose(fp);
  225.     }
  226. else DoMethod(mui_app,MUIM_Chess_ShowThinking,"Can't save file.");
  227. }
  228.